package com.umeox.watch.moto.launcher.adapter

import android.content.Context
import android.graphics.Color
import android.text.format.DateFormat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.recyclerview.widget.RecyclerView
import com.umeox.watch.moto.launcher.R
import com.umeox.watch.moto.launcher.bean.PrayerTimeInfo
import com.umeox.watch.moto.launcher.utils.Utils
import java.util.*

class PrayerTimeListAdapter(
    private val context:Context,
    private var datas: MutableList<PrayerTimeInfo>?,
    private var is24HourFormat: Boolean
) : RecyclerView.Adapter<PrayerTimeListAdapter.ItemViewHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val itemView =
            LayoutInflater.from(parent.context).inflate(R.layout.item_prayer_time, parent, false)
        return ItemViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val prayerTimeInfo = datas?.get(position)
        prayerTimeInfo?.let { holder.bind(it) }
    }

    override fun getItemCount(): Int {
        return datas?.size ?: 0
    }

    fun setData(prayerTimeList: MutableList<PrayerTimeInfo>) {
        if (datas == null) {
            datas = prayerTimeList
        } else {
            datas?.clear()
            datas?.addAll(prayerTimeList)
        }
        notifyDataSetChanged()
    }

    inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        private val mTvName: TextView = itemView.findViewById(R.id.name_tv)
        private val mTvTime: TextView = itemView.findViewById(R.id.time_tv)
        private val mPrayerTimeAmPm: TextView = itemView.findViewById(R.id.tv_am_pm)

        fun bind(prayerTimeInfo: PrayerTimeInfo) {
            mTvName.text = prayerTimeInfo.prayerTimeName
            is24HourFormat = DateFormat.is24HourFormat(context)
            if (is24HourFormat) {
                mTvTime.text = Utils.to24HourFormat(
                    prayerTimeInfo.prayerTime,
                    Locale.getDefault()
                )
                mPrayerTimeAmPm.visibility = View.GONE
            } else {
                mTvTime.text = Utils.to12HourFormat(
                    prayerTimeInfo.prayerTime,
                    Locale.getDefault()
                )
                mPrayerTimeAmPm.visibility = View.VISIBLE
                mPrayerTimeAmPm.text = prayerTimeInfo.timeType
            }

            if (prayerTimeInfo.isHighLight) {
                mTvTime.setTextColor(Color.parseColor("#16AF6A"))
                if (!is24HourFormat) {
                    mPrayerTimeAmPm.setTextColor(Color.parseColor("#16AF6A"))
                }
            }
        }
    }

}